import {getMaintainRecordDetail} from '@/base/api/ServiceApi';
import {Page} from '@/base/Page';
import {isEmpty} from '@/base/util/Strings';

class model1 {
  // eslint-disable-next-line camelcase
  FK_AllocateID = '0';
  PartName = '';
  Quantity = '';
  UnitPrice = '';
}

export class RecordDetailBlocBloc {
  model: any = {
    Breakdown_Description: '',
    ConsigneeName: '',
    DealerName: '',
    FK_AllocateID: '0',
    IsUnderWarranty: '0',
    LaborCost: '0.00',
    MaintenanceName: '',
    MaintenanceTime: '',
    PartsCost: '0.00',
    PostSaleID: '0',
    PostSaleNum: '',
    Remark: '',
    SerialNum: '',
    State: '0'
  };
  model2:any = {
    Attitude: '0',
    Clothing: '0',
    Times: '0',
    Comment: '',
    FK_PostSaleID: '0',
    Speciality: '0',
    Star: '0',
    SubmitTime: '0'
  };
  paged1: Page<model1> = new Page<model1>([], 0, 1, 10, 0, []);

  loading = false;
  fileList: Array<any> = [];
  fileList2: Array<any> = [];
  videoList: Array<any> = [];
  i: number = 0;
  num: number = 1;
  hasFormInfo = 0;
  hasFormInfo2 = 0;

  load(id: number) {
    this.loading = true;
    return getMaintainRecordDetail({id})
      .then((res) => {
        // eslint-disable-next-line no-eval
        let data = eval('(' + res.data + ')');
        // console.log(data);
        this.model = data.errMsg.Detail;
        if (!isEmpty(data.errMsg.Comment)) {
          this.model2 = data.errMsg.Comment;
        } else {
          this.num = 0;
        }
        this.paged1.items = data.errMsg.Results;
        if (!isEmpty(data.errMsg.Picture)) {
          Object.entries(data.errMsg.Picture).forEach(
            ([key, value]) => {
              let name = this.returnName(key);
              if (!isEmpty(value) && name !== 'workVideo') {
                // @ts-ignore
                this.fileList.push([name, 'https://dd.irestapp.com/order/backend/web/imgOrigin/' + value[0]])
              } else if (!isEmpty(value) && name === 'workVideo') {
                // @ts-ignore
                this.videoList.push(['维修视频', 'https://dd.irestapp.com/order/backend/web/imgOrigin/' + value[0]])
              }
            }
          );
          // console.log(this.fileList, this.videoList);
          // data.errMsg.Picture.map(x => console.log(x)
          //   // this.fileList.push('https://dd.irestapp.com/order/backend/web/imgOrigin/' + x.PictureUrl)
          // );
          // data.errMsg.Picture.forEach(x => console.log(x));
        } else {
         this.hasFormInfo += 1;
        }
        // if (!isEmpty(data.errMsg.Video)) {
        //   data.errMsg.Video.map(x => this.videoList.push('https://dd.irestapp.com/order/backend/web/imgOrigin/' + x.PictureUrl));
        // } else {
        //   this.hasFormInfo += 1;
        // }
        if (!isEmpty(data.errMsg.CommentPicture)) {
          data.errMsg.CommentPicture.map(x => this.fileList2.push('https://dd.irestapp.com/order/backend/web/imgOrigin/' + x.PictureUrl));
        } else {
          this.hasFormInfo2 += 1;
        }
      }).finally(() => {
        this.loading = false;
      })
  }
  shuchu(value) {
    let rating = '';
    for (let i = 0; i < value; i++) {
      rating += '★';
    }
    for (let a = 0; a < 5 - value; a++) {
      rating += '☆';
    }
    return rating;
  }
  returnName(name) {
    switch (name) {
      case 'ensure': return '服务确认单';
      case 'broken': return '产品故障位置';
      case 'serial': return '产品序列号';
      case 'resolve': return '解决结果';
      case 'recover': return '复原后全景';
    }
  }
}
